﻿using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.File;
using Catastrophe.BasePC.UI.Model;
using Catastrophe.BasePC.UI.SystemIni;
using Catastrophe.Initialize;
using Catastrophe.Initialize.IOCProvier;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static Org.BouncyCastle.Math.EC.ECCurve;

namespace Catastrophe.BasePC.UI.UIMode
{
    public partial class MainWindowModel : ObservableObject
    {
        /// <summary>
        /// 加载窗体
        /// </summary>
        public ICommand loadForm { get; set; }
        /// <summary>
        /// 切换显示左边菜单栏
        /// </summary>
        public ICommand switchLeftMenu { get; set; }

        /// <summary>
        /// 清空历史
        /// </summary>
        public ICommand ClearHisMenu { get; set; }
        /// <summary>
        /// 菜单
        /// </summary>
        private SysMenuDal _baseMenuNodeDal { get; set; }

        /// <summary>
        /// 左边宽
        /// </summary>
        [ObservableProperty]
        private int leftWidth;
        /// <summary>
        /// 左边菜单栏
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<MainWindowLeft> treeNodes;

        /// <summary>
        /// 历史点击的菜单
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<MainWindowLeft> clickHisMenu;
        /// <summary>
        /// 选中的左边菜单
        /// </summary>
        [ObservableProperty]
        private MainWindowLeft selectTreeNode;
        /// <summary>
        /// 窗口加载
        /// </summary>
        public Action<UserControl> UserControlLoad { get; set; }

        /// <summary>
        /// 配置
        /// </summary>
        public FileConfigEntity? config { get; set; }
        public MainWindowModel()
        {
            _baseMenuNodeDal = new SysMenuDal();
            loadForm = new RelayCommand<string>(loadFormMethod);
            switchLeftMenu = new RelayCommand(switchLeftMenuMethod);
            ClearHisMenu = new RelayCommand(ClearHisMenuMethod);
            TreeNodes = new ObservableCollection<MainWindowLeft>();
            ClickHisMenu = new ObservableCollection<MainWindowLeft>();
            config = FileConfigIni.GetFileConfig ;
            loadTreeData();
            IniConfig();
        }
        /// <summary>
        /// 设置配置
        /// </summary>
        private void IniConfig()
        {
            leftWidth = config.MenuWidth;
        }

        private void ClearHisMenuMethod()
        {
            for (int i = 0; i < ClickHisMenu.Count; i++)
            {
                if (ClickHisMenu[i].Entity.IsFixed==false)
                {
                    ClickHisMenu.RemoveAt(i);
                    i--;
                }
            }
        }

        /// <summary>
        /// 切换左边栏目显示/不显示
        /// </summary>
        private void switchLeftMenuMethod()
        {
            if (config.IsTreeNodeCollapse)
            {
                if (LeftWidth > 0)
                {
                    LeftWidth = 0;
                }
                else
                {
                    LeftWidth = 200;
                }
            }
        }
        /// <summary>
        /// 加载左边菜单数据
        /// </summary>
        private void loadTreeData()
        {
            TreeNodes.Clear();
            var allmenu = _baseMenuNodeDal.QueryBaseMenuNodeList("");
            var parents = allmenu.FindAll(x => x.ParentIndex == -1);
            foreach (var parent in parents)
            {
                MainWindowLeft leftnode = new MainWindowLeft();
                leftnode.ShowText = parent.NodeName;
                leftnode.Entity = parent;
                if (!string.IsNullOrEmpty(parent.ResourceName))
                {
                    leftnode.Clicked += Leftnode_Clicked;
                    if (parent.IsFixed)
                    {
                        ClickHisMenu.Add(leftnode);
                    }
                }
                leftnode.Nodes = new ObservableCollection<MainWindowLeft>();
                var nodes = allmenu.FindAll(x => x.ParentIndex == parent.BaseIndex);
                foreach (var node in nodes)
                {
                    MainWindowLeft leftnodes = new MainWindowLeft();
                    leftnodes.ShowText = node.NodeName;
                    leftnodes.Entity = node;
                    leftnodes.Clicked += Leftnode_Clicked;
                    leftnode.Nodes.Add(leftnodes);
                    if (node.IsFixed)
                    {
                        ClickHisMenu.Add(leftnodes);
                    }
                }
                TreeNodes.Add(leftnode);
            }
        }

        public void Leftnode_Clicked(SysMenuEntity e)
        {
            bool ishas = false;
            foreach (var node in clickHisMenu)
            {
                if (node.ShowText==e.NodeName)
                {
                    ishas = true;
                }
            }
            if (ishas==false)
            {
                MainWindowLeft leftnodes = new MainWindowLeft();
                leftnodes.ShowText = e.NodeName;
                leftnodes.Entity = e;
                leftnodes.Clicked += Leftnode_Clicked;
                ClickHisMenu.Add(leftnodes);
            }
            loadFormMethod(e);
        }

        private void loadFormMethod(string? obj)
        {
            var entity = _baseMenuNodeDal.QueryBaseResourceName(obj);
            if (entity == null)
            {
                MessageBox.Show("查找菜单失败");
                return;
            }
            loadFormMethod(entity);
        }
        private void loadFormMethod(SysMenuEntity entity)
        {
           
            BaseResponse<UserControl> byName = CatastropheProvider.GetByName<UserControl>(entity.Assembly.Trim(), entity.ResourceName);
            if (byName.Success)
            {
                UserControlLoad?.Invoke(byName.Content);
                if (byName.Content!=null)
                {
                    foreach (var node in ClickHisMenu)
                    {
                        if (node.ShowText == entity.NodeName)
                        {
                            node.IsSelected = true;
                        }
                        else
                        {
                            node.IsSelected = false;
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("ICO获取菜单失败");
                return;
            }
        }
    }
}
